﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Text;
using Frame.Lib.Utilities;
using Frame.Lib.Core.User;
using Frame.Lib.Core.Right;

public partial class Admin_User_SaveRight : System.Web.UI.Page
{
    private SystemRightBO _right;
    private SystemRightBO Right
    {
        get
        {
            if (_right == null)
            {
                _right  = new SystemRightBO(RightOwnerType.User);
            }
            return _right;
        }
    }
    private int UserID
    {
        get
        {
            return int.Parse(StrUtil.DecryptUrl(Request["UserID"]));
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        
        if (!IsPostBack)
        {

            BindView();
            if (UserID > 0)//管理员是小于0           
            {
                lblName.Text = (new UserBO()).GetName(UserID);
            }
        }
        SystemRightBO.RegisterRightHiddenField();
    }

    private void BindView()
    {
       
        DataTable dt = SystemRightBO.GetRightRect();
        StringBuilder sb = new StringBuilder();
        sb.Append("<table class='tbl1'>");
        foreach (DataRow dr in dt.Rows)
        {
            sb.Append("<tr>");
            sb.Append("<td class='head'>" + dr[0] + "：</td>");

            for (int i = 1; i < dt.Columns.Count; i++)
            {
                sb.Append("<td>" + dr[i] + "</td>");
            }

            sb.Append("</tr>");
        }
        sb.Append("</table>");

        tbl.Text = sb.ToString();


        #region 把此用户角色的所有权限取出来
        UserBO user = new UserBO();
        List<int> lstRoleIDs = user.GetRoleIDs(UserID);
        SystemRightBO tmp = new SystemRightBO(RightOwnerType.Role);
        List<int> lstRights = new List<int>();
        foreach (int roleID in lstRoleIDs)
        {
            lstRights.AddRange(tmp.GetRightIDs(roleID));
        }
        #endregion

        //用js控制用户已经有的权限为选中不可用的状态

        JSUtil.ExcuteJS("DisableSelectNode('" + StrUtil.MergeToStr(lstRights, ",") + "');");


        List<int> lstSelectIDs = Right.GetRightIDs(UserID);
        JSUtil.ExcuteJS("InitSelect('" + StrUtil.MergeToStr(lstSelectIDs, ",") + "');", "initSelect");


    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        List<int> lstRightIDs = StrUtil.SplitToInt(Request.Form["hidRightID"]);
        Right.SaveRight(UserID, lstRightIDs);
        string strScript = "window.close();returnValue=true";
        JSUtil.ResponseJS(strScript);
    }
}
